package com.wuzf.controller;


import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wuzf.utils.PageData;

@Controller
public class LoginController extends BaseController{
	protected Logger log = Logger.getLogger(this.getClass());
	
	
	//跳转到登陆界面
	@RequestMapping(value="/login")
	@ResponseBody
	public Object login() {
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("jsp/login");
		return mv;
	}
	
	
	//登陆
	@RequestMapping(value="/toLogin")
	@ResponseBody
	public Object toLogin(){
		ModelAndView mv = this.getModelAndView();
		try {
			PageData pd = this.getPageData();
			Subject subject = SecurityUtils.getSubject();
			UsernamePasswordToken token = 
					new UsernamePasswordToken(pd.getString("username"), pd.getString("password"));
			token.setRememberMe(pd.get("rememberMe") != null);
			subject.login(token);
			mv.setViewName("jsp/index");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			mv.addObject("item", e.getMessage());
			mv.setViewName("jsp/login");
		}
		return mv;
	}
	
	
}
